﻿using System;
using System.Windows;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCommon.Prompt;

namespace Baosight.ZNH.ZMCM.UI.ChildWindows
{
    public partial class TeamMaterialPlanDetail : EFViewCmes
    {
        public TeamMaterialPlanDetail(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
            Loaded += new RoutedEventHandler(TeamMaterialPlanDetail_Loaded);
        }

        void TeamMaterialPlanDetail_Loaded(object sender, RoutedEventArgs e)
        {
            if (base.Mode.Equals(OperationMode.Insert))
            {
                this.fdMaterialType.Value = "有编码物资";
                this.fdMaterialCode.eRegex = "^.+$";
                this.fdMaterialCode.eValidateErrorPrompt = "不能为空";
            }
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.InsertPanel.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="type"></param>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    this.fdMaterialCode.Visibility = Visibility.Collapsed;
                    this.fdMaterialCode.eBindingName = "result-0-other";
                    this.other.Visibility = Visibility.Visible;
                    this.other.eBindingName = "result-0-fdMaterialCode";                    
                    //BaseTools.EFDataFieldNotEnabled(this.InsertGrid.GetChildObjects<EFDataField>(null, true));
                    fdMaterialType.IsEnabled = false;
                    fdApplicationNum.IsReadOnly = false;
                    fdAuditNum.IsReadOnly = false;
                    fdPurchaseNum.IsReadOnly = false;
                    fdRemarkReport.IsReadOnly = false;
                    fdRemarkAudit.IsReadOnly = false;
                    fdRemarkPurchase.IsReadOnly = false;
                    fdSerialNum.IsReadOnly = false;
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            if (this.fdApplicationNum.Visibility == Visibility.Visible)
            {
                this.fdAuditNum.Value = this.fdApplicationNum.Value;
                this.fdPurchaseNum.Value = this.fdApplicationNum.Value;
            }
            if (this.fdAuditNum.Visibility == Visibility.Visible)
            {
                this.fdPurchaseNum.Value = this.fdAuditNum.Value;
            }
            base.OnCommitButtonClick("ZMCM23", sender, e);
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }



        private void fdMaterialCode_GotFocus(object sender, RoutedEventArgs e)
        {
            String str = (String)this.fdMaterialType.Value;

            if (str == null || str.Equals(""))
            {
                Prompts pop = Prompts.getInstance();
                pop.SetMsg("请先选择物资类型！");
                pop.Show(6);
                return;
            }
           
        }

        #region customview 值获取
        /// <summary>
        /// Customview 取值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdMaterialCode_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFDataField field = sender as EFDataField;
            CmesCustomView picker = field.Content as CmesCustomView;
            if (picker.ItemObject != null)
            {
                if (!picker.ItemObject.Equals(""))
                {
                    this.fdMaterialId.Value = picker.ItemObject.GetType().GetProperty("fdKeyId").GetValue(picker.ItemObject, null);
                    this.fdMaterialName.Value = picker.ItemObject.GetType().GetProperty("fdMaterialName").GetValue(picker.ItemObject, null);
                    this.fdMaterialCode.Value = picker.ItemObject.GetType().GetProperty("fdMaterialCode").GetValue(picker.ItemObject, null);
                    this.fdQuality.Value = picker.ItemObject.GetType().GetProperty("fdQuality").GetValue(picker.ItemObject, null);
                    this.fdSpecModel.Value = picker.ItemObject.GetType().GetProperty("fdSpecModel").GetValue(picker.ItemObject, null);
                    this.fdNumber.Value = picker.ItemObject.GetType().GetProperty("fdNumber").GetValue(picker.ItemObject, null);
                    this.fdMaterialInnerCode.Value = picker.ItemObject.GetType().GetProperty("fdMaterialInnerCode").GetValue(picker.ItemObject, null);
                    this.fdPrice.Value = picker.ItemObject.GetType().GetProperty("fdPrice").GetValue(picker.ItemObject, null);
                    this.fdMetric.Value = picker.ItemObject.GetType().GetProperty("fdMetric").GetValue(picker.ItemObject, null);
                    this.fdTypeCode.Value = picker.ItemObject.GetType().GetProperty("fdTypeAllCode").GetValue(picker.ItemObject, null);
                    (this.fdTypeCode.Content as CmesCustomView).Display = 
                        (picker.ItemObject.GetType().GetProperty("fdTypeAllCode").GetValue(picker.ItemObject, null).ToString()
                        + picker.ItemObject.GetType().GetProperty("fdMaterialType").GetValue(picker.ItemObject, null).ToString());
                    this.fdParameter.Value = picker.ItemObject.GetType().GetProperty("fdParameter").GetValue(picker.ItemObject, null);
                    var fdPrice = this.fdPrice.Value;
                    var fdApplicationNum = this.fdApplicationNum.Value;
                    double jg = 0;
                    if (fdPrice != null)
                    {
                        Double.TryParse(this.fdPrice.Value.ToString(), out jg);
                    }
                    double sqsl = 0;
                    if (fdApplicationNum != null)
                    {
                        Double.TryParse(this.fdApplicationNum.Value.ToString(), out sqsl);
                    }
                    double je = jg * sqsl;
                    this.fdSum.Value = je.ToString();
                }
                else
                {
                    this.fdMaterialCode.Value = "";
                    this.fdTypeCode.Value = "";
                    this.fdMaterialName.Value = "";
                    this.fdMetric.Value = "";
                    this.fdPrice.Value = "";
                    this.fdSpecModel.Value = "";
                    this.fdQuality.Value = "";
                    this.fdNumber.Value = "";
                    this.fdParameter.Value = "";
                    this.fdMaterialId.Value = "na";
                    this.fdMaterialInnerCode.Value = "";
                }
            }

            //throw new NotImplementedException();
        }
        #endregion

        private void fdMaterialType_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }
            if (e.NewValue.Equals("无编码物资"))
            {
                //物资编码
                this.other.Visibility = Visibility.Visible;
                this.other.Value = "";
                this.other.eBindingName = "result-0-fdMaterialCode";
                this.fdMaterialCode.Visibility = Visibility.Collapsed;
                this.fdMaterialCode.eBindingName = "result-0-other";
                this.fdMaterialCode.ValueChanged -= fdMaterialCode_ValueChanged;
                this.fdMaterialCode.eLabel = "物资编码";
                this.fdMaterialCode.IsReadOnly = true;
                this.fdMaterialCode.eRegex = null;
                this.fdMaterialCode.eValidateErrorPrompt = null;
                this.fdMaterialCode.Value = "";
                //物资分类编码
                this.fdTypeCode.Value = "";
                this.fdTypeCode.eLabel = "*物资分类编码";
                this.fdTypeCode.eRegex = "^.+$";
                this.fdTypeCode.eValidateErrorPrompt = "不能为空";                
                this.fdTypeCode.IsEnabled = true;
                (this.fdTypeCode.Content as CmesCustomView).Display = "";
                (this.fdTypeCode.Content as CmesCustomView).CleanPopupContent();
                //物资名称
                this.fdMaterialName.Value = "";
                this.fdMaterialName.eLabel = "*物资名称";
                this.fdMaterialName.eRegex = @"^.{1,40}$";
                this.fdMaterialName.eValidateErrorPrompt = "输入1-40个字符";                
                this.fdMaterialName.IsReadOnly = false;
                //规格型号
                this.fdSpecModel.Value = "";
                this.fdSpecModel.eLabel = "*规格型号";
                this.fdSpecModel.eRegex = "^.{1,50}$";
                this.fdSpecModel.eValidateErrorPrompt = "输入1-50个字符";
                this.fdSpecModel.IsReadOnly = false;
                //材质
                this.fdQuality.Value = "";
                this.fdQuality.eRegex = "^.{0,50}$";
                this.fdQuality.eValidateErrorPrompt = "输入0-50个字符";
                this.fdQuality.IsReadOnly = false;
                //计量单位
                this.fdMetric.Value = "";
                this.fdMetric.eLabel = "*计量单位";
                this.fdMetric.eRegex = "^.+$";
                this.fdMetric.eValidateErrorPrompt = "不能为空";                
                this.fdMetric.IsEnabled = true;
                //单价
                this.fdPrice.Value = "";
                this.fdPrice.eLabel = "*单价(元)";
                this.fdPrice.eRegex = "((^\\d{1,6}$)|(^\\d{1,6}\\.\\d{1,5}$)|(^0\\.\\d{1,5}$))";
                this.fdPrice.eValidateErrorPrompt = "输入整数位不超过6位，小数位不超过5位的数字";                
                this.fdPrice.IsReadOnly = false;
                //图号
                this.fdNumber.Value = "";
                this.fdNumber.eRegex = "^.{0,50}$";
                this.fdNumber.eValidateErrorPrompt = "输入0-50个字符";
                this.fdNumber.IsReadOnly = false;
                //技术指标
                this.fdParameter.Value = "";
                this.fdParameter.eRegex = "^.{0,100}$";
                this.fdParameter.eValidateErrorPrompt = "输入0-100个字符";
                this.fdParameter.IsReadOnly = false;

                //隐藏值
                this.fdMaterialId.Value = "na";
                this.fdMaterialInnerCode.Value = "";  
                
            }            
            else if (e.NewValue.Equals("有编码物资"))
            {
                //物资编码
                this.other.Visibility = Visibility.Collapsed;
                this.fdMaterialCode.Visibility = Visibility.Visible;
                this.other.eBindingName = "result-0-other";
                this.fdMaterialCode.Value = "";
                this.fdMaterialCode.eBindingName = "result-0-fdMaterialCode";
                this.fdMaterialCode.eLabel = "*物资编码";
                this.fdMaterialCode.ValueChanged += fdMaterialCode_ValueChanged;
                this.fdMaterialCode.eRegex = "^.+$";
                this.fdMaterialCode.eValidateErrorPrompt = "不能为空";
                (this.fdMaterialCode.Content as CmesCustomView).Display = "";
                (this.fdMaterialCode.Content as CmesCustomView).CleanPopupContent();
                this.fdMaterialCode.IsReadOnly = false;                
                //物资分类编码
                this.fdTypeCode.eLabel = "物资分类编码";
                this.fdTypeCode.eRegex = null;
                this.fdTypeCode.eValidateErrorPrompt = null;
                this.fdTypeCode.Value = "";
                this.fdTypeCode.Validate();
                this.fdTypeCode.IsEnabled = false;
                //物资名称
                this.fdMaterialName.eLabel = "物资名称";
                this.fdMaterialName.eRegex = null;
                this.fdMaterialName.eValidateErrorPrompt = null;
                this.fdMaterialName.Value = "";
                this.fdMaterialName.Validate();
                this.fdMaterialName.IsReadOnly = true;
                //规格型号
                this.fdSpecModel.Value = "";
                this.fdSpecModel.eLabel = "规格型号";
                this.fdSpecModel.eRegex = null;
                this.fdSpecModel.eValidateErrorPrompt = null;
                this.fdSpecModel.IsReadOnly = true;
                //材质
                this.fdQuality.Value = "";
                this.fdQuality.IsReadOnly = true;
                //计量单位
                this.fdMetric.eLabel = "计量单位";
                this.fdMetric.eRegex = null;
                this.fdMetric.eValidateErrorPrompt = null;
                this.fdMetric.Value = "";
                this.fdMetric.Validate();
                this.fdMetric.IsEnabled = false;
                //单价
                this.fdPrice.eLabel = "单价(元)";
                this.fdPrice.eRegex = null;
                this.fdPrice.eValidateErrorPrompt = null;
                this.fdPrice.Value = "";
                this.fdPrice.Validate();
                this.fdPrice.IsReadOnly = true;
                //图号
                this.fdNumber.Value = "";
                this.fdNumber.IsReadOnly = true;
                //技术指标
                this.fdParameter.Value = "";
                this.fdParameter.IsReadOnly = true;

                //隐藏值
                this.fdMaterialId.Value = "";
                this.fdMaterialInnerCode.Value = "";  
            }
            //Boolean validate = this.ValidateField();
        }

        /// <summary>
        /// 数量变化时，更新总价
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdApplicationNum_LostFocus(object sender, RoutedEventArgs e)
        {
            this.fdAuditNum.Value = this.fdApplicationNum.Value;
            this.fdPurchaseNum.Value = this.fdApplicationNum.Value;

            var fdPrice = this.fdPrice.Value;
            var fdApplicationNum = this.fdApplicationNum.Value;

            double jg = 0;
            if (fdPrice != null)
            {
                Double.TryParse(this.fdPrice.Value.ToString(), out jg);
            }
            double sqsl = 0;
            if (fdApplicationNum != null)
            {
                Double.TryParse(this.fdApplicationNum.Value.ToString(), out sqsl);
            }
            double je = jg * sqsl;
            this.fdSum.Value = je.ToString();
        }


        /// <summary>
        /// 工程改变，带出工程名
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdProjectname_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFDataField field = sender as EFDataField;
            CmesCustomView picker = field.Content as CmesCustomView;
            if (picker.ItemObject != null)
            {
                if (!picker.ItemObject.Equals(""))
                {
                    this.fdProjectId.Value = picker.ItemObject.GetType().GetProperty("fdKeyId").GetValue(picker.ItemObject, null);
                    this.fdProjectname.Value = picker.ItemObject.GetType().GetProperty("fdName").GetValue(picker.ItemObject, null);
                }
                else
                {
                    this.fdProjectId.Value = "";
                    this.fdProjectname.Value = "";
                }
            }
        }

        /// <summary>
        /// 单价变化时，更新总价
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdPrice_LostFocus(object sender, RoutedEventArgs e)
        {
            var fdPrice = this.fdPrice.Value;
            var fdPurchaseNum = this.fdPurchaseNum.Value;

            double jg = 0;
            if (fdPrice != null)
            {
                Double.TryParse(this.fdPrice.Value.ToString(), out jg);
            }
            double sqsl = 0;
            if (fdPurchaseNum != null)
            {
                Double.TryParse(this.fdPurchaseNum.Value.ToString(), out sqsl);
            }
            double je = jg * sqsl;
            this.fdSum.Value = je.ToString();
        }

        /// <summary>
        /// 审批数量修改后，修改申请金额
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdAuditNum_LostFocus(object sender, RoutedEventArgs e)
        {
            this.fdPurchaseNum.Value = this.fdAuditNum.Value;
            var fdPrice = this.fdPrice.Value;
            var fdAuditNum = this.fdAuditNum.Value;

            double jg = 0;
            if (fdPrice != null)
            {
                Double.TryParse(this.fdPrice.Value.ToString(), out jg);
            }
            double spsl = 0;
            if (fdAuditNum != null)
            {
                Double.TryParse(this.fdAuditNum.Value.ToString(), out spsl);
            }
            double je = jg * spsl;
            this.fdSum.Value = je.ToString();
        }

        /// <summary>
        /// 审核数量修改后，修改申请金额
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdPurchaseNum_LostFocus(object sender, RoutedEventArgs e)
        {
            var fdPrice = this.fdPrice.Value;
            var fdPurchaseNum = this.fdPurchaseNum.Value;

            double jg = 0;
            if (fdPrice != null)
            {
                Double.TryParse(this.fdPrice.Value.ToString(), out jg);
            }
            double shsl = 0;
            if (fdPurchaseNum != null)
            {
                Double.TryParse(this.fdPurchaseNum.Value.ToString(), out shsl);
            }
            double je = jg * shsl;
            this.fdSum.Value = je.ToString();
        }
    }
}
